package com.study.spwar.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Slf4j
@Controller
public class IndexController extends SysBaseController {
    @PostConstruct
    public void setBasePath() {
        try {
            String path = servletContext.getContextPath();
            log.info("系统路径和名称初始化工作:{}", path);
            servletContext.setAttribute("proPath", path);
        } catch (Exception e) {
            log.info("系统路径和名称初始化失败");
        }
    }

    @RequestMapping("/in")
    public String ind(HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("ds", new Date());
        model.addAttribute("name", "tomcat+JSP+SpringBoot");
        log.info(StringUtils.repeat("-", 20));
        String realPath = request.getServletContext().getRealPath("");
        log.info("getRealPath is {}", realPath);
        return "index";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("ds", new Date());
        model.addAttribute("name", "tomcat+JSP+SpringBoot+index");
        log.info(StringUtils.repeat("-", 20));
        return "index";
    }

    @RequestMapping("home")
    @ResponseBody
    public String homeindex(HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("ds", new Date());
        model.addAttribute("name", "tomcat+JSP+SpringBoot");
        log.info(StringUtils.repeat("-", 20));
        return "index" + new Date();
    }

    @RequestMapping("m2")
    public String m2(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "m2";
    }

}
